using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class BeePatrolState : BaseState {
   
    private Vector3 target;
    private Vector3 moveDir;

    public override void OnEnter(Enemy enemy) {
        Debug.Log("Bee Patrol");
        currentEnemy = enemy;
        currentEnemy.currentSpeed = currentEnemy.normalSpeed;
        target = enemy.GetNewPoint();
        currentEnemy.ani.SetBool("isFly", true);
    }

    public override void LogicUpdate() {
        // 如果发现Player则进入chase状态
        if (currentEnemy.FoundPlayer()) {
            currentEnemy.SwitchState(NPCState.Chase);
        }

        // 移动到目标点则进入等待状态，因为float小数精度的问题，我们不能直接等于target
        if (Mathf.Abs(target.x - currentEnemy.transform.position.x) < 0.1f && Mathf.Abs(target.y - currentEnemy.transform.position.y) < 0.1f) {
            currentEnemy.isWait = true;
            target = currentEnemy.GetNewPoint();
        }

        // 获得移动方向
        moveDir = (target - currentEnemy.transform.position).normalized;
        
        // 翻转Bee朝向
        if (moveDir.x > 0) {
            currentEnemy.transform.localScale = new Vector3(-1, 1, 1);
        }
        if (moveDir.x < 0) {
            currentEnemy.transform.localScale = new Vector3(1, 1, 1);
        }
    }

    public override void PhysicsUpdate() {
        if (!currentEnemy.isWait && !currentEnemy.isHurt && !currentEnemy.isDead) {
            currentEnemy.rb.velocity = moveDir * currentEnemy.currentSpeed * Time.deltaTime;
        } else {
            currentEnemy.rb.velocity = Vector2.zero;
        }
    }

    public override void OnExit() {
        currentEnemy.ani.SetBool("isFly", false);
        Debug.Log("Bee Patrol Exit");
    }

}
